<?php
class Khachhang_Model_Ajax extends Zend_Db_Table{
	
	protected $_name = 'khuyenmai';
	protected $_primary ='id';
	protected $_ids;
	
	public function getItem($arrParam = null, $options = null){
		$db = Zend_Registry::get('connectDb');
		//$db = Zend_Db::factory($adapter, $config);
		if($options['task'] == 'get-khoahoc') {
			$select = $db	-> select()
							-> from('nam_daotao_khoahoc AS kh')
							-> where('kh.id = ?', $arrParam['id_khoahoc']);
			$result = $db	-> fetchRow($select);
		}
		if($options['task'] == 'get-lophoc') {
			$select = $db	-> select()
							-> from('nam_daotao_lophoc AS lh')
							-> joinLeft('nam_hopdong AS hd', 'hd.id_lophoc = lh.id', array('COUNT(hd.id) AS total_hopdong'))
							-> where('lh.id = ?', $arrParam['id_lophoc'])
							-> group('lh.id');
			$result = $db	-> fetchRow($select);
		}
		if($options['task'] == 'get-khuyenmai') {
			$select = $db	-> select()
			-> from('khuyenmai AS km')
			-> where('km.id = ?', $arrParam['id_khuyenmai']);
			$result = $db	-> fetchRow($select);
		}
		
		return $result;
	}
	
	public function itemInSelectbox($arrParam = null, $options = null){
		$db = Zend_Registry::get('connectDb');
		//$db = Zend_Db::factory($adapter, $config);
		
		$quyen_nv = new Zendvn_System_Info();
		$quyen_nhom = $quyen_nv->getGroupInfo('permission');
		$quyen_info = $quyen_nv->getInfo();
		$quyen_member = $quyen_nv->getMemberInfo();
		$quyen_phongban = $quyen_nv->getPhongbanInfo();
		
		$date = new Zend_Date();
		$dateHomNay = $date->toString('YYYY-MM-dd');
		
		if($options['task'] == 'get-khoahoc'){
			$select = $db	->	select()
							->	from('nam_daotao_khoahoc AS kh',array('id','name'))
							->	where('kh.status = ?', 1)
							->	order('kh.order ASC');
			$result = $db	->	fetchPairs($select);
			
			$result[0] = '- Lựa chọn -';
			asort($result);
		}
		
		if($options['task'] == 'get-lophoc'){
			if (isset($arrParam['id_khoahoc'])) {
				$select = $db	->	select()
								->	from('nam_daotao_lophoc AS lh',array('id','name'))
								->	where('lh.status = ?', 1)
								->	where('lh.id_khoahoc = ?', $arrParam['id_khoahoc'])
								->	where('lh.trangthai != ?', 2)
								->	order('lh.order ASC');
				$result = $db	->	fetchPairs($select);
			} else {
				$result = array();
			}

			$result[0] = '- Lựa chọn -';
			asort($result);
		}
		
		if($options['task'] == 'get-khuyenmai'){
			$select = $db	->	select()
							->	from('khuyenmai AS km', array('id', 'name'))
							->	order('km.ngaybatdau ASC')
							->	order('km.id ASC');
							//->	where("DATEDIFF(km.ngaybatdau,'".$dateHomNay."') >= 0")
							//->	where("DATEDIFF(km.ngayketthuc,'".$dateHomNay."') <= 0");
			$result = $db	-> 	fetchPairs($select);
				
			$result[0] = '- Lựa chọn -';
			asort($result);
		}
		
		if($options['task'] == 'get-phongban') {
			$select = $db	->	select()
							->	from('phongban AS pb')
							->	order('pb.order ASC')
							->	order('pb.id ASC');
		
			$result = $db->fetchAll($select);
				
			$system = new Zendvn_System_Recursive($result);
			if(count($result) > 0) {
				if($quyen_nhom == 'Limit Access') {
					$result = $system->builArray($quyen_phongban['id'], 2);
					if(count($result) > 0) {
						$tmp = array('id' => $quyen_phongban['id'], 'name' => $quyen_phongban['name'], 'level' => 0, 'parents' => $quyen_phongban['parents'], 'order' => $quyen_phongban['order']);
						array_unshift($result, $tmp);
					} else {
						$result =  array('0' => array('id' => $quyen_phongban['id'], 'name' => $quyen_phongban['name'], 'level' => 0, 'parents' => $quyen_phongban['parents'], 'order' => $quyen_phongban['order']));
					}
				} else {
					$result = $system->builArray();
					$tmp = array('id' => 0, 'name' => '- Lựa chọn -', 'level' => 1, 'parents' => 0, 'order' => 1);
					array_unshift($result, $tmp);
				}
			} else {
				$result = array('0' => array('id' => 0, 'name' => '- Lựa chọn -', 'level' => 1, 'parents' => 0, 'order' => 1));
			}
		}
		
		if($options['task'] == 'get-nhanvien') {
			$select = $db 	-> select()
							-> from('users AS u',array('id','CONCAT(u.name," (",alias,")") AS name'))
							-> where('u.status = ?', 1)
							-> order('u.order ASC');

			if ($arrParam['id_phongban'] > 0) {
				$selectPhongban = $db -> select()
										-> from('phongban',array('id','name','parents'));
				$resultPhongban = $db->fetchAll($selectPhongban);
				
				$system = new Zendvn_System_Recursive($resultPhongban);
				$newArray = $system->builArray($arrParam['id_phongban']);
				$tmp[] = $arrParam['id_phongban'];
				if (count($newArray)>0) {
					foreach ($newArray as $key => $val) {
						$tmp[] = $val['id'];
					}
				}
				$ids = implode(',', $tmp);
				$select->where('u.id_phongban IN (' . $ids . ')');
				$result = $db -> fetchPairs($select);
				if(!empty($result[$quyen_member['id']])) {
					$result[$quyen_member['id']] = '- Tôi -';
				}
			} else {
				if($quyen_nhom == 'Limit Access') {
					$selectPhongban = $db	->	select()
											->	from('phongban AS pb')
											->	order('pb.order ASC')
											->	order('pb.id ASC');
				
					$resultPhongban = $db	->	fetchAll($selectPhongban);
				
					$system 	= new Zendvn_System_Recursive($resultPhongban);
					$newArray 	= $system->builArray($quyen_phongban['id']);
					$tmp[] = $quyen_phongban['id'];
					if (count($newArray)>0) {
						foreach ($newArray as $key => $val) {
							$tmp[] = $val['id'];
						}
					}
					$ids = implode(',', $tmp);
					if($ids != $quyen_phongban['id']) {
						$select->where('u.id_phongban IN (' . $ids . ')');
						$result = $db -> fetchPairs($select);
					}
					$result[$quyen_member['id']] = '- Tôi -';
				} else {	
					$result = array();
				}
			}
			
			$result[0] = '- Chọn nhân viên -';
			asort($result);
		}
		
		return $result;
	}
	
	public function listItem($arrParam = null, $options = null){
		$db = Zend_Registry::get('connectDb');
		//$db = Zend_Db::factory($adapter, $config);
		
		$quyen_nv = new Zendvn_System_Info();
		$quyen_nhom = $quyen_nv->getGroupInfo('permission');
		$quyen_info = $quyen_nv->getInfo();
		$quyen_member = $quyen_nv->getMemberInfo();
		$quyen_phongban = $quyen_nv->getPhongbanInfo();
		
		$date = new Zend_Date();
		$dateHomNay = $date->toString('YYYY-MM-dd');
		if($options['task'] == 'list-khuyenmai'){
			$select = $db	->	select()
							->	from('khuyenmai AS km', array('*', "DATE_FORMAT(ngaybatdau, '%d/%m/%Y %H:%i:%s') AS ngaybatdau", "DATE_FORMAT(ngayketthuc, '%d/%m/%Y %H:%i:%s') AS ngayketthuc", "REPLACE(FORMAT(giagiam,0),',','.') AS giagiam"))
							->	order('km.ngaybatdau ASC')
							->	order('km.id ASC');
			$result = $db	-> 	fetchAll($select);
		}
		
		if($options['task'] == 'admin-list'){
			$select = $db	->	select()
							->	from('khachhang AS kh', array('*', "DATE_FORMAT(kh.created, '%d/%m/%Y') AS created"))
							->	joinLeft('users AS u', 'u.id = kh.id_nhanvien', array('name AS nhanvien_name'))
							->	order('kh.created DESC')
							->	order('kh.id DESC')
							->	where('kh.status_tiemnang = ?', 1)
							->	where('kh.status != ?', -1)
							->	where('kh.status_khachhang = ?', 0)
							->	limit(7,0);
			
			if($quyen_nhom == 'Limit Access') {
				$select_category = $db	->	select()
										->	from('phongban AS pb', array('id','name','parents'));
					
				$resultCategory = $db	->	fetchAll($select_category);
					
				$system = new Zendvn_System_Recursive($resultCategory);
				$newArray = $system->builArray($quyen_phongban['id']);
				$tmp[] = $quyen_phongban['id'];
				if (count($newArray)>0) {
					foreach ($newArray as $key => $val) {
						$tmp[] = $val['id'];
					}
				}
				$ids = implode(',', $tmp);
				
				if($ids != $quyen_phongban['id']) {
					$select->where('u.id_phongban IN (' . $ids . ')');
					$result = $db -> fetchPairs($select);
				
					$selectUser	= $db	->	select()
										->	from('users AS u', array('id'))
										->	where('u.id_phongban IN(' . $ids . ')');
					$resultUser = $db	->	fetchCol($selectUser);
						
					if(count($resultUser) > 0) {
						$idUser = implode(',', $resultUser);
					} else {
						$idUser = 0;
					}
				} else {
					$idUser = $quyen_member['id'];
				}
					
				$select		->	where('kh.id_nhanvien IN(' . $idUser . ')');
			}
			
			$result = $db	-> 	fetchAll($select);
		}
	
		return $result;
	
	}
}